package com.cxb.view;

import com.cxb.R;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class DialogCreater {
	private View contentView;
	private Context context;
	private Dialog mDialog;
	private Window window;
	
	public DialogCreater(Context context)
	{
		this.context = context;
	}
	
	
	public Dialog getLoadingDialog(boolean showloading, String msg)
	{
		Dialog dialog = createLoadingDialog(context,msg);
		if(!showloading)
		{
			ImageView spaceshipImage = (ImageView) contentView.findViewById(R.id.img);
			spaceshipImage.setVisibility(View.GONE);
		}
		if(msg == null)
		{
			TextView tipTextView = (TextView) contentView.findViewById(R.id.tipTextView);
			tipTextView.setVisibility(View.GONE);
		}
		return dialog;
	}
	
	public Dialog getWifiDialog(Context context) {
		contentView = LayoutInflater.from(context).inflate(R.layout.wifi_connect_dialog, null);
		mDialog = new Dialog(context, R.style.dialog);
		mDialog.setContentView(contentView);
		window = mDialog.getWindow();
		LayoutParams p = window.getAttributes();
		p.width = LayoutParams.WRAP_CONTENT;
		p.width = LayoutParams.WRAP_CONTENT;
		window.setAttributes(p);
		TextView tv_confirm = (TextView) getView(R.id.tv_confirm);
		TextView tv_cancel = (TextView) getView(R.id.tv_cancel);
		return (mDialog);
	}
	
	public Dialog getCommonDialog(String title, String message ) {
		contentView = LayoutInflater.from(context).inflate(R.layout.common_dialog, null);
		mDialog = new Dialog(context, R.style.dialog);
		mDialog.setContentView(contentView);
		window = mDialog.getWindow();
		LayoutParams p = window.getAttributes();
		p.width = LayoutParams.WRAP_CONTENT;
		p.width = LayoutParams.WRAP_CONTENT;
		window.setAttributes(p);
		TextView tv_title = (TextView) getView(R.id.tv_title);
		TextView tv_message = (TextView) getView(R.id.tv_message);
		tv_message.setText(message);
		if(title == null)
		{
			tv_title.setVisibility(View.GONE);
			android.view.ViewGroup.LayoutParams mLayoutParams = tv_message.getLayoutParams();
			mLayoutParams.height = 50;
			tv_message.setGravity(Gravity.CENTER_VERTICAL);
			tv_message.setLayoutParams(mLayoutParams);
		}
		tv_title.setText(title);
		TextView tv_confirm = (TextView) getView(R.id.tv_confirm);
		TextView tv_cancel = (TextView) getView(R.id.tv_cancel);
		return (mDialog);
	}
	
	public Dialog getCommonDialog(int rtitle, int rmessage ) {
		String title = null;
		String message = null;
		if(rtitle != 0)
			title = context.getString(rtitle);
		if(rmessage !=0)
			message = context.getString(rmessage);
		return getCommonDialog(title,message);
	}
	
	public View getView(int resourceId) {
		if (contentView == null)
			return null;
		return contentView.findViewById(resourceId);
	}
	
	public  Dialog createLoadingDialog(Context context, String msg) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
		contentView = v;
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
				context, R.anim.loading_animation);
		// 使用ImageView显示动画
		tipTextView.setTextSize(tipTextView.getTextSize());
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		tipTextView.setText(msg);// 设置加载信息

		Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

		// loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
		return loadingDialog;

	}
	
	
	public Dialog getCommonDialog(String title, String message,final OnDialogOperateListener onDialogOperateListener ) 
	{
		final Dialog dialog = getCommonDialog(title,message);
		TextView tv_confirm = (TextView) getView(R.id.tv_confirm);
		TextView tv_cancel = (TextView) getView(R.id.tv_cancel);
		if(onDialogOperateListener != null)
		{
			tv_confirm.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(onDialogOperateListener != null)
						onDialogOperateListener.onConfirm(dialog);
				}
			});
			tv_cancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(onDialogOperateListener != null)
						onDialogOperateListener.onCancel(dialog);
				}
			});
		}
		return dialog;
	}
	
	public Dialog getCommonDialog(int rtitle, int rmessage, final OnDialogOperateListener onDialogOperateListener) {
		String title = null;
		String message = null;
		if(rtitle != 0)
			title = context.getString(rtitle);
		if(rmessage !=0)
			message = context.getString(rmessage);
		return getCommonDialog(title,message,onDialogOperateListener);
	}
	
	
	public interface OnDialogOperateListener
	{
		public void onConfirm(Dialog dilog);
		public void onCancel(Dialog dilog);
	}
}
